package com.example.stock.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.example.stock.comm.AssetEnum;
import com.example.stock.comm.Cons;
import com.example.stock.comm.FreqEnum;
import com.example.stock.entity.dao.ProBarCbDao;
import com.example.stock.entity.dao.ProBarFundDao;
import com.example.stock.entity.dao.ProBarIndexDao;
import com.example.stock.entity.dao.ProBarStockDao;
import com.example.stock.service.IServiceRowData;
import com.example.stock.vo.DataVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ServiceRowDataImpl implements IServiceRowData {

    @Resource
    private ProBarStockDao proBarStockDao;
    @Resource
    private ProBarFundDao proBarFundDao;
    @Resource
    private ProBarIndexDao proBarIndexDao;
    @Resource
    private ProBarCbDao proBarCbDao;

    @Override
    //statisticsBatch
    public List<DataVo> getRowDataWithFreq(String tsCode, List<String> tsCodes, LocalDateTime startTime, LocalDateTime endTime, FreqEnum freq, AssetEnum asset) {

        if (null == startTime) {
            startTime = Cons.year2001;
        }
        if (null == endTime) {
            endTime = LocalDateTime.now();
        }
        switch (asset) {
            case FUND:
                return proBarFundDao.getRowDataWithFreq(tsCode, tsCodes, startTime, endTime, freq);
            case INDEX:
                return proBarIndexDao.getRowDataWithFreq(tsCode,tsCodes, startTime, endTime, freq);
            case STOCK:
                return proBarStockDao.getRowDataWithFreq(tsCode,tsCodes, startTime, endTime, freq);
            case CB:
                return proBarCbDao.getRowDataWithFreq(tsCode, tsCodes,startTime, endTime, freq);
            default:
                throw new RuntimeException("枚举 asset 有误");
        }
    }

    @Override
    //statisticsBatch
    public Map<String, List<DataVo>> getRowDataWithFreqBatchGroupByCode(List<String> tsCodes, LocalDateTime startTime, LocalDateTime endTime, FreqEnum freq, AssetEnum asset, boolean dirtyQuery, int more) {
        Map<String, List<DataVo>> map = new HashMap<>();
        //重新计算开始查询时间
        startTime = recalculateStartTime(startTime,freq,more);
        for (String tsCode : tsCodes) {
            List<DataVo> rowDataWithFreq = getRowDataWithFreq(tsCode, null, startTime, endTime, freq, asset);
            if ((!dirtyQuery) && CollectionUtil.isEmpty(rowDataWithFreq)) {
                throw new RuntimeException("数据有误"+tsCode);
            }
            map.put(tsCode, rowDataWithFreq);
        }
        return map;
    }

    @Override
    public List<DataVo> getRowDataWithFreqOne(String tsCode, LocalDateTime startTime, LocalDateTime endTime, FreqEnum freq, AssetEnum asset, boolean dirtyQuery, int more) {
        startTime = recalculateStartTime(startTime,freq,more);
        List<DataVo> rowDataWithFreq = getRowDataWithFreq(tsCode, null, startTime, endTime, freq, asset);
        if ((!dirtyQuery) && CollectionUtil.isEmpty(rowDataWithFreq)) {
            throw new RuntimeException("数据有误"+tsCode);
        }
        return rowDataWithFreq;
    }


    //重新计算开始查询时间
    private LocalDateTime recalculateStartTime(LocalDateTime startTime, FreqEnum freq, int more) {
        LocalDateTime re;
        if (more > 0) {
            switch (freq) {
                case DAY:
                    re = startTime.plusDays(-more*9/5-5);
                    break;
                case MIN120:
                    re = startTime.plusDays(-(more/2)*9/5-5);
                    break;
                case MIN60:
                    re = startTime.plusDays(-(more/4)*9/5-5);
                    break;
                case MIN30:
                    re = startTime.plusDays(-(more/8)*9/5-5);
                    break;
                case MIN15:
                    re = startTime.plusDays(-(more/16)*9/5-5);
                    break;
                case MIN1:
                    re = startTime.plusDays(-(more/240)*9/5-5);
                    break;
                default:
                    throw new RuntimeException("freq有误");
            }
        }else{
            re = startTime;
        }
        return re;
    }

}
